package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub		
		request.setCharacterEncoding("UTF-8");    //设置输入内容的编码方式
		
		response.setContentType("text/html;charset=UTF-8");   //设置为html编码格式
		
		String username=request.getParameter("username");
		
		System.out.println(username);
		
		String password=request.getParameter("password");
		
		System.out.println(password);
		
	
		StringBuilder sb=new StringBuilder("<!DOCTYPE html>\n")
		.append("<html>\n")
		.append("<head>\n")
		.append("<meta charset=\"UTF-8\">\n")
		.append("</head>\n")
		.append("<body>\n")
		.append("username:"+username+"\n"+"<br>")
		.append("password:"+password+"\n")
		.append("</body>\n")		
		.append("</html>")
		;

		response.getWriter().append(sb.toString());

		//response.getWriter().append("\n用户名:"+username).append("\n密码:"+password);	
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");    //设置输入内容的编码方式
		
		response.setContentType("text/html;charset=UTF-8");   //设置为html编码格式
		
		String username=request.getParameter("username");
		
		System.out.println(username);
		
		String password=request.getParameter("password");
		
		System.out.println(password);
		
		request.setAttribute("username", username);
		request.setAttribute("password", password);
		
		//跳转
		request.getRequestDispatcher("login_result.jsp").forward(request, response);  //分发器
		//response.sendRedirect("IndexServlet");
	}

}
